package com.arraymasterx.util;

import com.arraymasterx.config.AlgorithmConfig;
import com.arraymasterx.exception.AlgorithmException;

public class MatrixUtils {

    public static void validateMatrix(int[][] matrix, AlgorithmConfig config) {
        if (matrix == null) {
            throw new AlgorithmException("Matrix cannot be null");
        }

        if (matrix.length == 0) {
            throw new AlgorithmException("Matrix must have at least one row");
        }

        if (matrix.length > config.getMatrix().getMaxDimension()
                || matrix[0].length > config.getMatrix().getMaxDimension()) {
            throw new AlgorithmException(
                    "Matrix dimension exceeds maximum allowed size: " + config.getMatrix().getMaxDimension());
        }

        // 验证所有行长度一致
        int firstRowLength = matrix[0].length;
        for (int i = 1; i < matrix.length; i++) {
            if (matrix[i].length != firstRowLength) {
                throw new AlgorithmException("All rows must have the same length");
            }
        }
    }

    public static boolean isSquareMatrix(int[][] matrix) {
        if (matrix == null || matrix.length == 0)
            return false;
        return matrix.length == matrix[0].length;
    }
}